package android.framework.rpc.convertor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Represent Class or Field
 * The class should verify that it'nt represent both Class and Field simultaneously/
 * @author Shahar
 *
 */
public class MemberInfo implements Iterable<MemberInfo>{
	private List<MemberInfo> members;
	private String name;
	private Class<?> type;
	private String value;
	private boolean isCollection;
	private int arrayLength;
	// for verify just one member type representation (Class or Field) propose
	private boolean isClass;
	
	private MemberInfo(Class<?> type){
		this.type = type;
		members = new ArrayList<MemberInfo>();
	}
	
	public MemberInfo(Class<?> type, List<MemberInfo> members) {
		this(type);
		this.setClass(true);
		this.setMembers(members);
	}
	
	public MemberInfo(Class<?> type, String value) {
		this(type);
		this.setClass(false);
		this.setValue(value);
		
	}
	
	public Class<?> getType() {
		return type;
	}

	public void setType(Class<?> type) {
		this.type = type;
	}

	public Iterator<MemberInfo> iterator() {
		return getMembers().iterator();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		if(isClass()){
			throw new IllegalArgumentException("can't represent both Field and Class, check that you use in the right constructor");
		}
		this.value = value;
	}

	public List<MemberInfo> getMembers() {
		return members;
	}

	public void setMembers(List<MemberInfo> members) {
		if(!isClass()){
			throw new IllegalArgumentException("can't represent both Field and Class, check that you use in the right constractor");
		}
		this.members = members;
	}

	public boolean isClass() {
		return isClass;
	}

	public void setClass(boolean isClass) {
		this.isClass = isClass;
	}
	
	public String toString(){
		StringBuilder sb = new StringBuilder();
		for (MemberInfo member : this) {
			sb.append(member.toString() + "\n");
		}
		return String.format("isClass:%s\nType:%s\nValue:%s\nName:%s\nMembers:%s",
				isClass(), getType().getName(), getValue(), getName(), sb.toString());
	}

	public boolean isCollection() {
		return isCollection;
	}

	public void setCollection(boolean isCollection) {
		this.isCollection = isCollection;
	}

	public int getArrayLength() {
		return arrayLength;
	}

	public void setArrayLength(int arrayLength) {
		this.arrayLength = arrayLength;
	}
}
